package com.cskaoyan.market.listener;

import org.apache.ibatis.io.Resources;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebListener
public class ApplicationListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("app.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            String upload = properties.getProperty("uploadBasePath");
            String domain = properties.getProperty("domain");
            System.out.println(upload);
            ServletContext servletContext = servletContextEvent.getServletContext();
            servletContext.setAttribute("upload",upload);
            servletContext.setAttribute("domain",domain);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
